<?php

declare(strict_types=1);

namespace AdminBundle\Services;

use AdminBundle\Repositories\AdminRepository;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use Throwable;
use Hyperf\Di\Annotation\Inject;
use AdminBundle\Services\RolesService;
class AdminService
{
    /**
     * @Inject
     * @var RolesService
     */
    public $rolesService;

    public function getList(array $filter, int $page = 1, int $pageSize = 20, array $order = ['created_at'=>'DESC'])
    {
        return AdminRepository::instance()->getList($filter, ['id', 'account', 'username', 'mobile', 'email', 'status', 'last_login_time', 'last_login_ip', 'created_at'], $page, $pageSize, $order);
    }

    public function getInfo($id, array $cols = ['*'])
    {
        $info = AdminRepository::instance()->find($id, $cols);
        $info['user_roles'] = $this->rolesService->getUserRolesId($id);
        return $info;
    }

    public function getAdminInfo($id, array $cols = ['*'])
    {
        $info = AdminRepository::instance()->find($id, $cols);
        $info['user_roles'] = $this->rolesService->getUserRoles($id);
        return $info;
    }

    public function getRolesList()
    {
        return $this->rolesService->getList([], 0);
    }

    public function register(array $data)
    {
        try {
            foreach ($data as $key => &$v) {
                if (!isset($data[$key])) {
                    unset($data[$key]);
                }
            }
            $tmp = [];
            if (isset($data['user_roles'])) {
                if (!is_array($data['user_roles'])) {
                    $data['user_roles'] = explode(',', $data['user_roles']);
                }
                $tmp = $data['user_roles'];
                unset($data['user_roles']);
            }
            $user = AdminRepository::instance()->saveData($data);
            $this->rolesService->deleteRolesUser(['user_id' => $user['id']]);
            $time = date('Y-m-d H:i:s',time());
            $saveRoles = [];
            foreach ($tmp as $v) {
                $saveRoles[] = [
                    'system_role_id' => $v,
                    'user_id' => $user['id'],
                    'created_at' => $time,
                    'updated_at' => $time,
                ];
            }
            $this->rolesService->saveUserRoles($saveRoles);
        } catch (Throwable $e) {
            echo $e->getMessage();
            throw new BusinessException(ErrorCode::SERVER_ERROR, '注册失败');
        }

        return true;
    }

    /**
     * deleteInfo
    * 删除信息
    * @param $id
    * @return mixed
    */
    public function deleteInfo($id)
    {
        AdminRepository::instance()->deleteByIds($id);
        $where = ['user_id' => $id];
        $this->rolesService->deleteRolesUser($where);
        return true;
    }

    /**
     * 修改密码
     */
    public function changePwd(array $data)
    {
        //验证密码
        $user = AdminRepository::instance()->find($data['id'], ['password']);
        if (empty($user)) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, '账号不存在');
        }
        if (!password_verify($data['old_password'], $user['password'])) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, '密码错误');
        }

        unset($data['old_password']);
        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        AdminRepository::instance()->saveData($data);

        return true;
    }

    public function __call($name, $arguments)
    {
        return AdminRepository::instance()->$name(...$arguments);
    }
}